home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / srgp / src / grx / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-03  |  10.7 KB  |  344 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3. #include <ctype.h>
  4.  
  5. /*
  6.  * if this is defined some BIOS keycodes are remapped for the SUIT
  7.  * text and line editor widgets
  8.  */
  9. #define MAP_KEYS_FOR_SUIT
  10.  
  11. #define BACKSPACE_KEY     8
  12. #define CARRIAGE_RETURN  13
  13.  
  14. static int kb_enable = 0;
  15. static int ms_enable = 0;
  16. static int in_wait_event;
  17. static int previous_key = FALSE;
  18.  
  19. #ifdef __GNUC__
  20. # define clock()    rawclock()
  21. #endif
  22.  
  23. void SRGP__initInputDrivers(void)
  24. {
  25. #ifdef DEBUG
  26.     MouseEventMode(FALSE);
  27. #else
  28.     MouseEventMode(TRUE);
  29. #endif
  30.     MouseInit();
  31.     kb_enable = 0;
  32.     ms_enable = 0;
  33.     MouseEventEnable(kb_enable,ms_enable);
  34.     srgpx__starttime = clock();
  35. }
  36.  
  37. void SRGP__updateInputSelectionMask(void)
  38. {
  39.     kb_enable = (srgp__cur_mode[KEYBOARD] == INACTIVE) ? 0 : 1;
  40.     ms_enable = (srgp__cur_mode[LOCATOR]  == INACTIVE) ? 0 : 1;
  41.     MouseEventEnable(kb_enable,ms_enable);
  42. }
  43.  
  44. /**
  45.  ** RAW-LEVEL DEACTIVATION OF A DEVICE
  46.  **   Responsible for erasing echo, and resetting device's measure to the
  47.  **    hardwired default.
  48.  **   Upon entry, the device's cur_mode is its old value (has not been
  49.  **    changed yet)!  And this procedure does not change it!
  50.  **/
  51. void SRGP__deactivateDevice(int device)
  52. {
  53.     switch(device) {
  54.       case LOCATOR:
  55.         SRGP__disableLocatorRubberEcho();
  56.         SRGP__disableLocatorCursorEcho();
  57.         srgp__cur_locator_measure.position = SRGP_defPoint(
  58.         (srgp__canvasTable[0].max_xcoord >> 1),
  59.         (srgp__canvasTable[0].max_ycoord >> 1)
  60.         );
  61.         ms_enable = 0;
  62.         MouseEventEnable(kb_enable,ms_enable);
  63.         break;
  64.       case KEYBOARD:
  65.         SRGP__disableKeyboardEcho();
  66.         srgp__cur_keyboard_measure.buffer[0] = '\0';
  67.         srgp__cur_keyboard_measure_length = 0;
  68.         bzero(srgp__cur_keyboard_measure.modifier_chord,
  69.         sizeof(srgp__cur_keyboard_measure.modifier_chord)
  70.         );
  71.         kb_enable = 0;
  72.         MouseEventEnable(kb_enable,ms_enable);
  73.         previous_key = FALSE;
  74.         break;
  75.     }
  76. }
  77.  
  78. /**
  79.  ** RAW-LEVEL ACTIVATION OF A DEVICE
  80.  **  Called whenever:
  81.  **    a device is placed into EVENT or SAMPLE mode...
  82.  **      a) when previously inactive
  83.  **      b) when previously active but in a different mode
  84.  **  Upon entry, the device's echo info and mode has already
  85.  **    been set to their new values.
  86.  **  Responsible for initiating echo
  87.  **/
  88. void SRGP__activateDevice(int device)
  89. {
  90.     switch(device) {
  91.       case LOCATOR:
  92.         SRGP__disableLocatorCursorEcho();
  93.         SRGP__disableLocatorRubberEcho();
  94.         SRGP__enableLocatorCursorEcho();
  95.         SRGP__enableLocatorRubberEcho();
  96.         SRGP__updateInputSelectionMask();
  97.         ms_enable = 1;
  98.         break;
  99.       case KEYBOARD:
  100.         SRGP__enableKeyboardEcho();
  101.         SRGP__updateInputSelectionMask();
  102.         previous_key = FALSE;
  103.         kb_enable = 1;
  104.         break;
  105.     }
  106.     MouseEventEnable(kb_enable,ms_enable);
  107. }
  108.  
  109. void SRGP__updateRawCursorPosition(void)
  110. {
  111.     srgp__cur_Xcursor_x = srgp__cur_locator_measure.position.x;
  112.     srgp__cur_Xcursor_y = SCREENFIXED(srgp__cur_locator_measure.position.y);
  113.     MouseWarp(srgp__cur_Xcursor_x,srgp__cur_Xcursor_y);
  114. }
  115.  
  116. void SRGP__updateLocationKnowledge(void)
  117. {
  118.     srgp__dirty_location = FALSE;         /* don't bother calling me!! */
  119.     return;
  120. }
  121.  
  122. static inputDevice HandleButtonEvent(MouseEvent *e)
  123. {
  124.     int which_button = (-1);
  125.  
  126.     if(e->flags & (M_LEFT_UP | M_LEFT_DOWN)) {
  127.         if(srgp__cur_locator_button_mask & LEFT_BUTTON_MASK)
  128.         which_button = LEFT_BUTTON;
  129.         srgp__cur_locator_measure.button_chord[LEFT_BUTTON] =
  130.         (e->buttons & M_LEFT) ? DOWN : UP;
  131.     }
  132.     if(e->flags & (M_MIDDLE_UP | M_MIDDLE_DOWN)) {
  133.         if(srgp__cur_locator_button_mask & MIDDLE_BUTTON_MASK)
  134.         which_button = MIDDLE_BUTTON;
  135.         srgp__cur_locator_measure.button_chord[MIDDLE_BUTTON] =
  136.         (e->buttons & M_MIDDLE) ? DOWN : UP;
  137.     }
  138.     if(e->flags & (M_RIGHT_UP | M_RIGHT_DOWN)) {
  139.         if(srgp__cur_locator_button_mask & RIGHT_BUTTON_MASK)
  140.         which_button = RIGHT_BUTTON;
  141.         srgp__cur_locator_measure.button_chord[RIGHT_BUTTON] =
  142.         (e->buttons & M_RIGHT) ? DOWN : UP;
  143.     }
  144.     if(which_button >= 0)
  145.         srgp__cur_locator_measure.button_of_last_transition = which_button;
  146.     srgp__cur_locator_measure.modifier_chord[SHIFT] =
  147.         ((e->kbstat & KB_SHIFT) ? TRUE : FALSE);
  148.     srgp__cur_locator_measure.modifier_chord[CONTROL] =
  149.         ((e->kbstat & KB_CTRL) ? TRUE : FALSE);
  150.     srgp__cur_locator_measure.modifier_chord[META] =
  151.         ((e->kbstat & KB_ALT) ? TRUE : FALSE);
  152.     srgp__cur_Xcursor_x = e->x;
  153.     srgp__cur_Xcursor_y = e->y;
  154.     srgp__cur_locator_measure.position.x = e->x;
  155.     srgp__cur_locator_measure.position.y = SCREENFIXED(e->y);
  156.     if(srgp__cur_mode[LOCATOR] != EVENT)
  157.         return(NO_DEVICE);
  158.     if(which_button < 0)
  159.         return(NO_DEVICE);
  160.     if(!in_wait_event)
  161.         return(NO_DEVICE);
  162.     srgp__get_locator_measure = srgp__cur_locator_measure;
  163.     return(LOCATOR);
  164. }
  165.  
  166. static inputDevice HandleRawModeKeyEvent(MouseEvent *e)
  167. {
  168. #ifdef MAP_KEYS_FOR_SUIT
  169.     switch(e->key) {
  170.       case 0x148:
  171.       case 0x248:                /* Up */
  172.         srgp__cur_keyboard_measure.buffer[0] = 'P' - 0x40;
  173.         break;
  174.       case 0x150:
  175.       case 0x250:                /* Down */
  176.         srgp__cur_keyboard_measure.buffer[0] = 'N' - 0x40;
  177.         break;
  178.       case 0x14b:
  179.       case 0x24b:                /* Left */
  180.         srgp__cur_keyboard_measure.buffer[0] = 'B' - 0x40;
  181.         break;
  182.       case 0x14d:
  183.       case 0x24d:                /* Right */
  184.         srgp__cur_keyboard_measure.buffer[0] = 'F' - 0x40;
  185.         break;
  186.       case 0x153:
  187.       case 0x253:                /* Del */
  188.         srgp__cur_keyboard_measure.buffer[0] = 'D' - 0x40;
  189.         break;
  190.       case 0x151:
  191.       case 0x251:                /* PgDown */
  192.         srgp__cur_keyboard_measure.buffer[0] = 'V' - 0x40;
  193.         break;
  194.       case 0x149:
  195.       case 0x249:                /* PgUp */
  196.         srgp__cur_keyboard_measure.buffer[0] = 'v';
  197.         e->kbstat |= KB_ALT;
  198.         break;
  199.       case 0x147:
  200.       case 0x247:                /* Home */
  201.         srgp__cur_keyboard_measure.buffer[0] = 'A' - 0x40;
  202.         break;
  203.       case 0x14f:
  204.       case 0x24f:                /* End */
  205.         srgp__cur_keyboard_measure.buffer[0] = 'E' - 0x40;
  206.         break;
  207.       default:
  208.         srgp__cur_keyboard_measure.buffer[0] = e->key;
  209.         break;
  210.     }
  211. #else
  212.     srgp__cur_keyboard_measure.buffer[0] = e->key;
  213. #endif
  214.     srgp__cur_keyboard_measure.buffer[1] = '\0';
  215.     srgp__cur_keyboard_measure.modifier_chord[SHIFT] =
  216.         ((e->kbstat & KB_SHIFT) ? TRUE : FALSE);
  217.     srgp__cur_keyboard_measure.modifier_chord[CONTROL] =
  218.         ((e->kbstat & KB_CTRL) ? TRUE : FALSE);
  219.     srgp__cur_keyboard_measure.modifier_chord[META] =
  220.         ((e->kbstat & KB_ALT) ? TRUE : FALSE);
  221.     if(srgp__cur_mode[KEYBOARD] == EVENT) {
  222.         strcpy(srgp__get_keyboard_measure.buffer,srgp__cur_keyboard_measure.buffer);
  223.         srgp__get_keyboard_measure.position = srgp__cur_keyboard_measure.position;
  224.         bcopy(srgp__cur_keyboard_measure.modifier_chord,
  225.         srgp__get_keyboard_measure.modifier_chord,
  226.         sizeof(srgp__get_keyboard_measure.modifier_chord)
  227.         );
  228.         if(in_wait_event) return(KEYBOARD);
  229.         previous_key = TRUE;
  230.     }
  231.     return(NO_DEVICE);
  232. }
  233.  
  234. static inputDevice HandleProcModeKeyEvent(MouseEvent *e)
  235. {
  236.     switch(e->key) {
  237.       case CARRIAGE_RETURN:
  238.         if(srgp__cur_mode[KEYBOARD] == EVENT) {
  239.         strcpy(srgp__get_keyboard_measure.buffer,srgp__cur_keyboard_measure.buffer);
  240.         srgp__get_keyboard_measure.position = srgp__cur_keyboard_measure.position;
  241.         }
  242.         srgp__cur_keyboard_measure.buffer[0] = '\0';
  243.         srgp__cur_keyboard_measure_length = 0;
  244.         SRGP__updateKeyboardEcho();
  245.         if(srgp__cur_mode[KEYBOARD] == EVENT) {
  246.         if(in_wait_event) return(KEYBOARD);
  247.         previous_key = TRUE;
  248.         }
  249.         break;
  250.       case BACKSPACE_KEY:
  251.         if(srgp__cur_keyboard_measure_length > 0) {
  252.         srgp__cur_keyboard_measure_length = srgp__cur_keyboard_measure_length - 1;
  253.         srgp__cur_keyboard_measure.buffer[srgp__cur_keyboard_measure_length] = '\0';
  254.         SRGP__updateKeyboardEcho();
  255.         }
  256.         break;
  257.      default:
  258.         /* CHECK: IS THE KEY PRINTABLE ASCII? */
  259.         if(isprint(e->key) && (srgp__cur_keyboard_measure_length < MAX_STRING_SIZE)) {
  260.         srgp__cur_keyboard_measure.buffer[srgp__cur_keyboard_measure_length] = e->key;
  261.         srgp__cur_keyboard_measure_length++;
  262.         srgp__cur_keyboard_measure.buffer[srgp__cur_keyboard_measure_length] = '\0';
  263.         SRGP__updateKeyboardEcho();
  264.         }
  265.         break;
  266.     }
  267.     return(NO_DEVICE);
  268. }
  269.  
  270. /**
  271.  ** SRGP__handleRawEvents
  272.  **  "This function nevers enters a wait state, unless it has been
  273.  **     called as a result of SRGP_waitEvent(FOREVER).
  274.  **   It examines all the events on the "raw"
  275.  **     queue: the queue of the underlying graphics package
  276.  **     (e.g., X11, Mac).
  277.  **   Exception: it may not handle all the raw events.
  278.  **     It exits as soon as it sees a valid trigger situation.
  279.  **   It returns a device ID IF AND ONLY IF...
  280.  **     1) the appl. is in a call to SRGP_waitEvent(), AND
  281.  **     2) a valid trigger for a device currently in Event mode
  282.  **        has been encountered.
  283.  **   IF it does return a device ID, THEN...
  284.  **     It automatically sets the proper value for either
  285.  **         srgp__get_locator_measure or
  286.  **         srgp__get_keyboard_measure
  287.  **     in preparation for the application's ensuing call to
  288.  **         SRGP_get...()
  289.  **   Another exception: it may "pass over" some raw events and
  290.  **     just leave them in the raw queue.
  291.  **   It will pass over a raw event IF AND ONLY IF...
  292.  **     1) the appl. is not in a call to SRGP_waitEvent(), AND
  293.  **     2) the event is a valid trigger for a device
  294.  **        currently in Event mode.
  295.  **   Another possibility is that it will discard a raw event
  296.  **     without processing it at all.
  297.  **   It will discard a raw event IF AND ONLY IF...
  298.  **     The event is for a device that is currently inactive."
  299.  **   FOR GRX IT WILL NOT PUSH BACK ANY EVENTS!!!
  300.  **   IT SIMPLY DISCARDS ANYTHING FROM THE QUEUE WHICH DOES NOT
  301.  **   MATCH
  302.  **/
  303. int SRGP__handleRawEvents(boolean inwaitevent,boolean forever)
  304. {
  305.     MouseEvent  evt;
  306.     inputDevice id;
  307.     int flags = M_NOPAINT;
  308.  
  309.     if(inwaitevent && previous_key) {
  310.         previous_key = FALSE;
  311.         return(KEYBOARD);
  312.     }
  313.     if(kb_enable) {
  314.         flags = (M_KEYPRESS | M_NOPAINT);
  315.         if(srgp__cur_mode[KEYBOARD] == SAMPLE) flags |= M_POLL;
  316.     }
  317.     if(ms_enable) {
  318.         flags |= (M_MOTION | M_BUTTON_CHANGE);
  319.         if(srgp__cur_mode[LOCATOR] == SAMPLE) flags |= M_POLL;
  320.     }
  321.     if(!forever || !inwaitevent) flags |= M_POLL;
  322.     in_wait_event = inwaitevent;
  323.     for( ; ; ) {
  324.         MouseGetEvent(flags,&evt);
  325.         if(evt.flags & M_KEYPRESS) {
  326.         srgpx__cur_time = evt.time;
  327.         srgp__cur_keyboard_measure.position = srgp__cur_locator_measure.position;
  328.         if(srgp__cur_keyboard_processing_mode == RAW) {
  329.             if((id = HandleRawModeKeyEvent(&evt)) != NO_DEVICE) return(id);
  330.             continue;
  331.         }
  332.         if((id = HandleProcModeKeyEvent(&evt)) != NO_DEVICE) return(id);
  333.         continue;
  334.         }
  335.         if(evt.flags & (M_MOTION | M_BUTTON_CHANGE)) {
  336.         srgpx__cur_time = evt.time;
  337.         if((id = HandleButtonEvent(&evt)) != NO_DEVICE) return(id);
  338.         continue;
  339.         }
  340.         if(!forever || !inwaitevent) return(NO_DEVICE);
  341.     }
  342. }
  343.  
  344.